package org.czp.sockect;

import java.io.IOException;

import org.eclipse.jetty.websocket.WebSocket.OnTextMessage;
import org.eclipse.jetty.websocket.WebSocket.OnBinaryMessage;

class TextWebSocket implements OnBinaryMessage, OnTextMessage {

	private final Transport transport;
	private Connection _connection;

	TextWebSocket(Transport transport) {
		this.transport = transport;
	}

	public void onClose(int closeCode, String message) {
		this.transport.members.remove(this);
	}

	public void sendMessage(String data) throws IOException {
		_connection.sendMessage(data);
	}

	public void onMessage(String data) {
		this.transport.listener.onTextMessage(data);
	}

	public boolean isOpen() {
		return _connection.isOpen();
	}

	public void onOpen(Connection connection) {
		try {
			this.transport.members.add(this);
			_connection = connection;
			connection.sendMessage("Conn server success");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onMessage(byte[] arg0, int arg1, int arg2) {
		this.transport.listener.onBinMessage(arg0);
	}

	public void sendBinMessage(byte[] obj) throws IOException {
		_connection.sendMessage(obj, 0, obj.length);
	}
}